package com.tb.service;

import com.tb.mapper.StarMapper;
import com.tb.model.Star;
import com.tb.model.StarExample;
import com.tb.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: wangguichun
 * @create: 2018-04-14 23:50
 **/

@Service
public class StarService {

    @Autowired
    private StarMapper starMapper;
    @Autowired
    private UserService userService;

    public List<User> getUserListByQuestionId(Integer questionId) {
        //根据问题id获取关注记录
        StarExample example = new StarExample();
        StarExample.Criteria criteria = example.createCriteria();
        criteria.andQuestionIdEqualTo(questionId);
        //根据关注记录中的用户id获取用户列表
        List<Star> starList = starMapper.selectByExample(example);
        List<User> userList = new ArrayList<>();
        for (Star star : starList) {
            userList.add(userService.getUserById(star.getUserId()));
        }
        return userList;
    }


    public void addStarRecord(Star star) {
        starMapper.insertSelective(star);
    }

    public void removeStarRecord(Star star) {
        StarExample example = new StarExample();
        StarExample.Criteria criteria = example.createCriteria();
        criteria.andQuestionIdEqualTo(star.getQuestionId());
        criteria.andUserIdEqualTo(star.getUserId());

        starMapper.deleteByExample(example);
    }

    public List<Star> getStarListByUserId(int myUserId) {
        StarExample example = new StarExample();
        StarExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(myUserId);

        return starMapper.selectByExample(example);
    }
}
